﻿using System;
using SlimDX.DirectInput;

namespace TileGert
{
  /// <summary>
  /// GertJoystick determines the state of a single joystick.
  /// Don't use this class directly! Use the GetJoystickInput class instead!
  /// </summary>
  public class GertJoystick
  {
    private JoystickState _state = null;
    
    /// <summary>
    /// Creates a new joystick state.
    /// </summary>
    public GertJoystick()
    {
    }
    
    /// <summary>
    /// Gets or sets the joystick state that is used to get the joysticks data.
    /// </summary>
    internal JoystickState State
    {
      get { return _state; }
      set { _state = value; }
    }
    
    /// <summary>
    /// Gets the X value of the joystick. Ranges from 0 - 65535 where 0 means right and 65535 means right.
    /// </summary>
    public int X
    {
      get
      {
        if (_state != null)
          return _state.X;
        else
          return 0;
      }
    }
    
    /// <summary>
    /// Gets the Y value of the joystick. Ranges from 0 - 65535 where 0 means top and 65535 means bottom.
    /// </summary>
    public int Y
    {
      get
      {
        if (_state != null)
          return _state.Y;
        else
          return 0;
      }
    }
    
    /// <summary>
    /// Gets the Z value of the joystick. Normally this is something like the X axis of a second joystick or a slider.
    /// </summary>
    public int Z
    {
      get
      {
        if (_state != null)
          return _state.Z;
        else
          return 0;
      }
    }
    
    /// <summary>
    /// Gets the Acceleration of the X axis of the joystick.
    /// </summary>
    public int AccelerationX
    {
      get
      {
        if (_state != null)
          return _state.AccelerationX;
        else
          return 0;
      }
    }
    
    /// <summary>
    /// Gets the Acceleration of the Y axis of the joystick.
    /// </summary>
    public int AccelerationY
    {
      get
      {
        if (_state != null)
          return _state.AccelerationY;
        else
          return 0;
      }
    }
    
    /// <summary>
    /// Gets the Acceleration of the Z axis of the joystick.
    /// </summary>
    public int AccelerationZ
    {
      get
      {
        if (_state != null)
          return _state.AccelerationZ;
        else
          return 0;
      }
    }
    
    /// <summary>
    /// Gets the Angular Acceleration of the X axis of the joystick.
    /// </summary>
    public int AngularAccelerationX
    {
      get
      {
        if (_state != null)
          return _state.AngularAccelerationX;
        else
          return 0;
      }
    }
    
    /// <summary>
    /// Gets the Angular Acceleration of the Y axis of the joystick.
    /// </summary>
    public int AngularAccelerationY
    {
      get
      {
        if (_state != null)
          return _state.AngularAccelerationY;
        else
          return 0;
      }
    }
    
    /// <summary>
    /// Gets the Angular Acceleration of the Z axis of the joystick.
    /// </summary>
    public int AngularAccelerationZ
    {
      get
      {
        if (_state != null)
          return _state.AngularAccelerationZ;
        else
          return 0;
      }
    }
    
    /// <summary>
    /// Gets the Angular Velocity of the X axis of the joystick.
    /// </summary>
    public int AngularVelocityX
    {
      get
      {
        if (_state != null)
          return _state.AngularVelocityX;
        else
          return 0;
      }
    }
    
    /// <summary>
    /// Gets the Angular Velocity of the Y axis of the joystick.
    /// </summary>
    public int AngularVelocityY
    {
      get
      {
        if (_state != null)
          return _state.AngularVelocityY;
        else
          return 0;
      }
    }
    
    /// <summary>
    /// Gets the Angular Velocity of the Z axis of the joystick.
    /// </summary>
    public int AngularVelocityZ
    {
      get
      {
        if (_state != null)
          return _state.AngularVelocityZ;
        else
          return 0;
      }
    }
    
    /// <summary>
    /// Gets the Forc of the X axis of the joystick.
    /// </summary>
    public int ForceX
    {
      get
      {
        if (_state != null)
          return _state.ForceX;
        else
          return 0;
      }
    }
    
    /// <summary>
    /// Gets the Forc of the Y axis of the joystick.
    /// </summary>
    public int ForceY
    {
      get
      {
        if (_state != null)
          return _state.ForceY;
        else
          return 0;
      }
    }
    
    /// <summary>
    /// Gets the Forc of the Z axis of the joystick.
    /// </summary>
    public int ForceZ
    {
      get
      {
        if (_state != null)
          return _state.ForceZ;
        else
          return 0;
      }
    }
    
    /// <summary>
    /// Gets the Rotation of the X axis of the joystick.
    /// </summary>
    public int RotationX
    {
      get
      {
        if (_state != null)
          return _state.RotationX;
        else
          return 0;
      }
    }
    
    /// <summary>
    /// Gets the Rotation of the Y axis of the joystick.
    /// </summary>
    public int RotationY
    {
      get
      {
        if (_state != null)
          return _state.RotationY;
        else
          return 0;
      }
    }
    
    /// <summary>
    /// Gets the Rotation of the Z axis of the joystick.
    /// </summary>
    public int RotationZ
    {
      get
      {
        if (_state != null)
          return _state.RotationZ;
        else
          return 0;
      }
    }
    
    /// <summary>
    /// Gets the number of buttons this joystick has.
    /// </summary>
    public int NumberOfButtons
    {
      get
      {
        if (_state != null)
          return _state.GetButtons().Length;
        else
          return 0;
      }
    }
    
    /// <summary>
    /// Checks if the given button is pressed.
    /// </summary>
    /// <param name="button">The number of the button.</param>
    /// <returns>True if the button is pressed, else false.</returns>
    public bool IsButtonPressed(int button)
    {
      if (_state != null)
        return _state.IsPressed(button);
      else
        return false;
    }
    
    /// <summary>
    /// Checks if the given button is released.
    /// </summary>
    /// <param name="button">The number of the button.</param>
    /// <returns>True if the button is released, else false.</returns>
    public bool IsButtonReleased(int button)
    {
      if (_state != null)
        return _state.IsReleased(button);
      else
        return false;
    }
    
    /// <summary>
    /// Gets the angle of the Point-Of-View-Controller with the given index.
    /// </summary>
    /// <param name="index">The index of the Point-Of-View-Controller.</param>
    /// <returns>The angle the controller points to, where 0 means up and all 
    /// other values are clockwise angles, or -1 if the contoller is not pressed.</returns>
    public int PointOfViewController(int index)
    {
      if (_state != null)
      {
        int[] controllers = _state.GetPointOfViewControllers();
        if (index < controllers.Length)
          return controllers[index];
      }
      return 0;
    }

    /// <summary>
    /// Gets the number of Point-Of-View-Controllers this Joystick has.
    /// </summary>
    public int NumberOfPointOfViewControllers
    {
      get
      {
        if (_state != null)
          return _state.GetPointOfViewControllers().Length;
        else
          return 0;
      }
    }
    
  }
}
